package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.common.Language
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.util.LanguageConfig
import com.youdianstar.app.databinding.LayoutItemIndustryBinding
import com.youdianstar.app.entity.OpIndustry
import com.youdianstar.app.common.ex.parseTx


/**
 * File: IndustryAdapter
 * Author: 82149 Create: 2023/8/4 10:49
 * Changes (from 2023/8/4)
 * --------------------------------------------------
 * description: 行业列表
 * ---------------------------------------------------
 *
 */
class IndustryAdapter(val context: Context, var dataList: MutableList<OpIndustry>, val clickListener: ItemSelectListener)
    : RecyclerView.Adapter<IndustryAdapter.ListHolder>(){

    class ListHolder(val binding : LayoutItemIndustryBinding) : RecyclerView.ViewHolder(binding.root)

    interface ItemSelectListener{
        fun onItemAction(index:Int)
    }

    var languageNow :String = ""
    init {
        languageNow = LanguageConfig.instance.getLanguageCode()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder {
        return ListHolder(LayoutItemIndustryBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: ListHolder, position: Int) {
        dataList[position].let { item ->
            if (null!=item.industryName){
                when(languageNow){
                    Language.ES->{
                        holder.binding.tvName.text = item.industryName.es.parseTx()
                    }
                    Language.ZH->{
                        holder.binding.tvName.text = item.industryName.zh.parseTx()
                    }
                    else ->{
                        holder.binding.tvName.text = item.industryName.en.parseTx()
                    }
                }
            }else{
                holder.binding.tvName.text = "--"
            }

            if (null!=item.description){
                when(languageNow){
                    Language.ES->{
                        holder.binding.tvDes.text = item.description.es.parseTx()
                    }
                    Language.ZH->{
                        holder.binding.tvDes.text = item.description.zh.parseTx()
                    }
                    else ->{
                        holder.binding.tvDes.text = item.description.en.parseTx()
                    }
                }
                holder.binding.tvDes.visibility = View.VISIBLE
            }else{
                holder.binding.tvDes.visibility = View.INVISIBLE
            }

            holder.binding.lineItem.visibility = if (position == dataList.size-1) View.INVISIBLE else View.VISIBLE
        }

        holder.binding.configContainer.setThrottleListener {
            clickListener.onItemAction(holder.bindingAdapterPosition)
        }
    }
}